package com.kcyu.sfhinvoice.service.impl;

import cn.hutool.core.io.resource.ClassPathResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kcyu.sfhinvoice.domain.Invoice;
import com.kcyu.sfhinvoice.domain.User;
import com.kcyu.sfhinvoice.service.EmailService;
import com.kcyu.sfhinvoice.service.UserService;
import com.kcyu.sfhinvoice.utils.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;

import javax.mail.MessagingException;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private UserService userService;


    @Override
    public boolean sendNewInvoiceEmail(Serializable invoiceId) throws IOException {
        ArrayList<String> emailAddress = new ArrayList<>();
        emailAddress.add("944472692@qq.com");

        Invoice invoice = new Invoice().selectById(invoiceId);

        TemplateEngine templateEngine = new TemplateEngine();
        StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
        stringTemplateResolver.setTemplateMode(TemplateMode.HTML);
        templateEngine.setTemplateResolver(stringTemplateResolver);
        Context context = new Context();
        context.setVariable("id", invoice.getId());
        context.setVariable("buyer", invoice.getBuyer());
        context.setVariable("taxNumber", invoice.getTaxNumber());
        context.setVariable("bankAccount", invoice.getBankAccount());
        context.setVariable("invoiceCount", invoice.getInvoiceCount());

        ClassPathResource classPathResource = new ClassPathResource("templates/invoiceNotice.html");
        InputStream inputStream = classPathResource.getStream();
        String templateContent = "";
        String htmlString = "";
        try (ByteArrayOutputStream result = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            templateContent = result.toString(StandardCharsets.UTF_8.name());
            htmlString = templateEngine.process(templateContent, context);
        }
        String finalHtmlString = htmlString;
        emailAddress.forEach(each -> {
            try {
                emailUtil.sendMessage(each, "收到一条新的发票工单", finalHtmlString);
            } catch (UnsupportedEncodingException | MessagingException e) {
                log.error("发送邮件异常！:{}", e);
            }
        });
        return true;
    }
}
